package com.think.teaching.mapper;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lowagie.text.List;
import com.think.teaching.model.ExpStat;

public interface ExpStatMapper extends BaseMapper<ExpStat> {
	
	
	@Select("<script>SELECT exp_stat.*,student_info.true_name,vr_lesson.exp_name FROM `exp_stat`,student_info,vr_lesson WHERE student_info.user_id=exp_stat.user_id AND vr_lesson.id = exp_stat.exp_id "
			+ "<if test='exp gt 0'>  AND exp_id =#{exp}</if>"
//			+ "<if test='start != null'>  AND start_time >#{start}</if>"
//			+ "<if test='end !=null'>  AND end_time <#{end}</if>"
			+ " ORDER BY exp_stat.start_time DESC  "
			+ "</script>")
	IPage<Map<String, Object>> ListStat(IPage<Map<String, Object>> page,
										@Param("exp")Integer exp,
										@Param("start")Timestamp start,
										@Param("end")Timestamp end);
	
	@Select("<script>SELECT exp_stat.*,student_info.true_name,vr_lesson.exp_name FROM `exp_stat`,student_info,vr_lesson WHERE student_info.user_id=exp_stat.user_id AND vr_lesson.id = exp_stat.exp_id "
			+ "<if test='id gt 0'>  AND exp_id IN (SELECT vl.id FROM vr_lesson vl,chapter WHERE chapter.course_id=#{id} AND vl.chapter_id=chapter.id AND chapter.lesson_type=3)</if>"
			+ " ORDER BY exp_stat.start_time DESC  "
			+ "</script>")
	IPage<Map<String, Object>> ListStatByCourse(IPage<Map<String, Object>> page,@Param("id")Integer id);

	@Select("<script>SELECT exp_stat.*,student_info.true_name,vr_lesson.exp_name FROM `exp_stat`,student_info,vr_lesson WHERE student_info.user_id=exp_stat.user_id AND vr_lesson.id = exp_stat.exp_id AND exp_stat.user_id=#{userId}"
			+ "<if test='exp gt 0'>  AND exp_id =#{exp}</if>"
			+ " ORDER BY exp_stat.start_time DESC  "
			+ "</script>")
	IPage<Map<String, Object>> ListUserStat(IPage<Map<String, Object>> page,
											@Param("exp")Integer exp,
											@Param("userId")Integer userId);
	
	@Select("SELECT sum(time_used) FROM exp_stat WHERE exp_id=#{expId} AND status=3")
	Long GetTotalTime(Integer expId);
	
	@Select("SELECT Count(*) FROM exp_stat WHERE time_used<=#{max} AND time_used>#{min} AND exp_id=#{expId} AND status=3")
	Integer GetStatTime(
			@Param("min") Integer min ,
			@Param("max") Integer max,
			@Param("expId") Integer expId);
	
	@Select(" SELECT id,exp_id,user_id,learn_content,status,start_time,end_time,time_used,add_id,score,created_by,created_time,updated_by,updated_time,delflag,state FROM exp_stat WHERE (start_time >= NOW()-INTERVAL 2 HOUR AND user_id = #{userId} AND status != 3 AND state = 1 AND exp_id = #{expId} AND delflag = 0) Limit 1")
	ExpStat GetLastRecord(
			@Param("userId") Integer userId,
			@Param("expId") Integer expId);
	
	@Select("SELECT  COUNT(1) FROM `exp_stat` WHERE exp_id=#{expId} GROUP BY user_id")
	ArrayList<Integer> GetExpStudent(@Param("expId")Integer expId);
	
	
	@Select("<script>SELECT exp_stat.id,exp_stat.exp_id,learn_content,start_time,end_time,time_used,add_id,status,exp_stat.score,student_info.true_name,vr_lesson.exp_name,student_info.student_id FROM `exp_stat`,student_info,vr_lesson WHERE student_info.user_id=exp_stat.user_id AND vr_lesson.id = exp_stat.exp_id "
			+ "<if test='exp gt 0'>  AND exp_id =#{exp}</if>"
			+ "<if test='start != null'>  AND start_time &gt; #{start}</if>"
			+ "<if test='end !=null'>  AND end_time &lt;#{end}</if>"
			+ " ORDER BY exp_stat.start_time DESC  "
			+ "</script>")
	java.util.List<Map<String, Object>> getAllListStat(
			@Param("exp") Integer exp,
			@Param("start") Timestamp start,
			@Param("end") Timestamp end);
}
